package com.csdj.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csdj.music.vo.Consumer;
import com.csdj.music.service.ConsumerService;
import com.csdj.music.mapper.ConsumerMapper;
import com.csdj.music.vo.RespBean;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
* @author ZouLi
* @description 针对表【consumer】的数据库操作Service实现
* @createDate 2023-03-13 15:47:53
*/
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper, Consumer>
    implements ConsumerService{

    /**
     * ConsumerMapper
     **/
    @Autowired(required = false)
    private ConsumerMapper consumerMapper;

    /**
     * RedisTemplate
     **/
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * passwordEncodeer
     **/
    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * @Description: 手机号注册
     * @Param [consumer, code]
     * @Return com.csdj.music.vo.RespBean
     * @Throws
     **/
    @Override
    public RespBean registConsumer(Consumer consumer, String code) {
        int count = consumerMapper.selectCount(new QueryWrapper<Consumer>().eq("phone_num", consumer.getPhone_num()));
        if (count > 0){
            return RespBean.error("手机号已被注册！");
        }

        ValueOperations<String, Object> redis = redisTemplate.opsForValue();
        String registCode = redis.get("registCode").toString();
        if (!StringUtils.equals(registCode, code)){
            return RespBean.error("验证码错误！");
        }

        Date date = new Date();
        consumer.setCreate_time(date);
        consumer.setUpdate_time(date);

        // 密码加密
        consumer.setPassword(passwordEncoder.encode(consumer.getPassword()));
        if (consumerMapper.insert(consumer) > 0) {
            return RespBean.success("注册成功！");
        }
        return RespBean.error("注册失败！");
    }

    /**
     * @Description: 账号密码登录
     * @Param [username, password, code, request]
     * @Return com.csdj.music.vo.RespBean
     * @Throws
     **/
    @Override
    public RespBean login(String username, String password, String code, HttpServletRequest request) {
        String captcha = (String) request.getSession().getAttribute("fontCode");
        if (!StringUtils.equals(captcha, code)){
            return RespBean.error("验证码错误！");
        }

        Consumer consumer = consumerMapper.selectOne(new QueryWrapper<Consumer>().eq("username", username));
        if (consumer == null){
            return RespBean.error("用户不存在！");
        }

        if (passwordEncoder.matches(password,consumer.getPassword())){
            return RespBean.success("登录成功！");
        }
        return RespBean.error("账号或密码错误！");
    }

    /**
     * @Description: 手机号登录
     * @Param [phone, code]
     * @Return com.csdj.music.vo.RespBean
     * @Throws
     **/
    @Override
    public RespBean login(String phone, String code) {
        ValueOperations<String, Object> redis = redisTemplate.opsForValue();
        String loginCode = redis.get("loginCode").toString();
        if (!StringUtils.equals(loginCode,code)){
            return RespBean.error("验证码错误！");
        }

        Consumer consumer = consumerMapper.selectOne(new QueryWrapper<Consumer>().eq("phone_num", phone));
        if (consumer != null){
            return RespBean.success("登录成功！");
        }

        return RespBean.error("手机号不存在！");
    }
}




